﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log.config", ConfigFileExtension = "config", Watch = true)]

namespace RkLibs.Log
{
    public static class LogMgr
    {
        /// <summary>
        /// 默认日志分类
        /// </summary>
        private const string DEFAULT_LOGGER = "logger";
        /// <summary>
        /// 默认日志等级
        /// </summary>
        private const LogLevel DEFAULT_LEVEL = LogLevel.INFO;
        /// <summary>
        /// 写入日志
        /// </summary>
        /// <param name="logger">日志分类</param>
        /// <param name="message">日志内容</param>
        /// <param name="level">日志等级</param>
        public static void WriteLog(string logger, string message, LogLevel level = DEFAULT_LEVEL)
        {
            ILog log = LogManager.GetLogger(logger);
            switch (level)
            {
                case LogLevel.INFO:
                    if (log.IsInfoEnabled)
                    {
                        log.Info(message);
                    }
                    break;
                case LogLevel.ERROR:
                    if (log.IsErrorEnabled)
                    {
                        log.Error(message);
                    }
                    break;
                default:
                    break;
            }
        }
        /// <summary>
        /// 写入日志
        /// </summary>
        /// <param name="logger">日志分类</param>
        /// <param name="message">日志内容</param>
        /// <param name="ex">错误</param>
        /// <param name="level">日志等级</param>
        public static void WriteLog(string logger, string message, Exception ex, LogLevel level = DEFAULT_LEVEL)
        {
            message += "\t" + ex.Message;
            WriteLog(logger, message, level);
        }
        /// <summary>
        /// 写入日志
        /// </summary>
        /// <param name="message">日志内容</param>
        /// <param name="level">日志等级</param>
        public static void WriteLog(string message, LogLevel level = DEFAULT_LEVEL)
        {
            WriteLog(DEFAULT_LOGGER,message,level);
        }
        /// <summary>
        /// 写入日志
        /// </summary>
        /// <param name="ex">错误</param>
        /// <param name="level">日志等级</param>
        public static void WriteLog(Exception ex, LogLevel level = DEFAULT_LEVEL)
        {
            WriteLog(DEFAULT_LOGGER, ex, level);
        }
        /// <summary>
        /// 写入日志
        /// </summary>
        /// <param name="message">日志内容</param>
        /// <param name="ex">错误</param>
        /// <param name="level">日志等级</param>
        public static void WriteLog(string message, Exception ex, LogLevel level = DEFAULT_LEVEL)
        {
            WriteLog(DEFAULT_LOGGER, message, ex, level);
        }
    }
}
